﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using GUI.SimpleControls;

namespace StandardScriptItems.SimpleControls
{
    public class EditableComboBox : ComboBox

    {
        TextBox Box;
        int counter = 0;

        public EditableComboBox() : this("", new List<object>())
        {
            
        }

        public EditableComboBox(String text, IEnumerable<Object> options)
        {
            Name = "EditableComboBox";
            Box = new TextBox();
            Box.Name = "text";
            Box.Text = text;
            ComboBoxItem textBoxItem = new ComboBoxItem();
            textBoxItem.Name = "edit";
            textBoxItem.Content = Box;
            textBoxItem.IsSelected = true;
            Add(textBoxItem);
            foreach (object option in options)
            {
                Add(option);
            }
        }

        public void Clear()
        {
            while(Items.Count > 1)
                Items.RemoveAt(1);
        }

        public virtual ComboBoxItem Add(object obj)
        {
            ComboBoxItem item;
            if (obj is ComboBoxItem)
            {
                item = obj as ComboBoxItem;
            }
            else
            {
                item = new ComboBoxItem();
                item.Content = obj;
                item.Name = "Item_" + counter++;
                if (obj is IObervable)
                {
                    (obj as IObervable).OnUpdate += delegate()
                    {
                        item.Content = obj.ToString();
                    };
                }
            }
            Items.Add(item);
            return item;
        }

        public Object this[int i]
        {
            get
            {
                return Items[i + 1];
            }
            set
            {
                Items[i + 1] = value;
            }
        }

        public ItemCollection Items { get { return base.Items; } }
        //public String Text { get { return ToString(); } set { Box.Text = value; } }

        public override string ToString()
        {
            if (SelectedIndex <= 0)
                return Box.Text;
            else
            {
                return (SelectedItem as ComboBoxItem).Content.ToString();
            }
        }
    }
}


